package com.eddy.controller;


import com.eddy.entity.DataDecrypt;
import com.eddy.entity.User;
import com.eddy.service.LoginService;
import com.eddy.util.DataEncryptUtils;
import com.eddy.util.DataUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {
    private static Logger log = Logger.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String login(@RequestBody DataDecrypt decrypt){
        try {
            //解密请求
            User user = DataEncryptUtils.decryptParameter(decrypt, User.class);
            //登录
            String password = DataEncryptUtils.shaEncrypt(user.getPassword());
            User massage = loginService.getUserMessage(user.getUsername(), password);
            if(massage != null) {
                //加密响应数据
                String result = DataEncryptUtils.encryptData(massage,"OK");
                return result;
            }
        }catch (Exception e){
            log.error("Login Exception:",e);
        }
        return DataUtils.resultData("Error");
    }
}
